package net.csdn.business.common.domain.request;

import com.google.common.collect.Lists;
import lombok.Data;
import lombok.EqualsAndHashCode;
import net.csdn.business.common.domain.common.ConditionQueryDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Positive;
import javax.validation.constraints.Size;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author likun
 * @date 2022/6/17 22:42
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class OrgIssueQueryDto extends IssueBaseQueryDto {
    @Positive(message = "projectId 不合法")
    private Long projectId;

    @NotBlank(message = "orgPath 不能为空")
    private String orgPath;

    /**
     * 如果没有选择项目的话，需要查询 云效 接口获取项目ID
     */
    private List<Long> projectIds;

    /**
     * 创建者筛选
     */
    @Size(min = 24, max = 24, message = "createUid 不合法")
    private String createUid;


    public static ConditionQueryDto transformQuery(OrgIssueQueryDto dto) {
        ConditionQueryDto query = transformCommon(dto);
        wrapperStatus(query, dto);
        query.setOrderBy(order(dto));
        return query;
    }

    public static ConditionQueryDto transformCount(OrgIssueQueryDto dto) {
        return transformCommon(dto);
    }

    private static ConditionQueryDto transformCommon(OrgIssueQueryDto dto) {
        ConditionQueryDto query = queryCommonCondition(dto);

        List<ConditionQueryDto.Filter> filters = query.getFilters();
        if (!CollectionUtils.isEmpty(dto.getProjectIds())) {
            filters.add(new ConditionQueryDto.Filter("project_id", dto.getProjectIds().stream().map(Object::toString).collect(Collectors.toList())));
        }
        if (StringUtils.isNotBlank(dto.getCreateUid())) {
            filters.add(new ConditionQueryDto.Filter("uid", Lists.newArrayList(dto.getCreateUid())));
        }
        return query;
    }
}